import axios from 'axios';
import mpAdapter from 'axios-miniprogram-adapter';
import {useUserInfo} from '@/stores/userInfo.js';
axios.defaults.adapter = mpAdapter;
import {netConfig} from '@/config/net.config';
const {baseURL,contentType} = netConfig;
let tokenLose = true;
const userStore = useUserInfo();

const instance = axios.create({
	baseURL,
	headers: {
		'Content-Type': contentType,
	},
});

// request interceptor
// console.log('axios请求拦截器'),
instance.interceptors.request.use(
	(config) => {
		return config;
	},
	(error) => {
		return Promise.reject(error);
	}
);

// response interceptor
instance.interceptors.response.use(
	/**
	 * If you want to get http information such as headers or status
	 * Please return  response => response
	 */
	(response) => {
		const res = response.data;
		if (!res.success) {
			uni.hideLoading();
			uni.showToast({
				title: res.message,
				icon: 'error',
				duration: 2000,
			});
		}
		return res;
	},
	(error) => {
		uni.hideLoading();
		uni.showToast({
			title: '服务器异常',
			icon: 'error',
			duration: 2000,
		});
		return Promise.reject(error);
	}
);
export default instance;